<script setup>
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores'
import { registerApi } from '@/api/user'
const userStore = useUserStore()
const router = useRouter()
const route = useRoute()

// 登录表单
const loginFormData = ref({
  account: '',
  password: '',
  isAgree: false
})

const isRegister = ref(false)
// 注册表单
const registerFormData = ref({
  account: '',
  password: '',
  comfirmPass: '',
  isAgree: false
})

// 校验规则
const loginRules = {
  account: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 16, message: '用户名长度为3-16位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度为6-16位', trigger: 'blur' }
  ],
  isAgree: [
    {
      validator: (rule, value, callback) => {
        if (!value) {
          callback(new Error('请勾选同意隐私条款和服务条款'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const registerRules = ref({
  account: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 16, message: '用户名长度为3-16位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度为6-16位', trigger: 'blur' }
  ],
  comfirmPass: [
    { required: true, message: '请输入确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (
          registerFormData.value.password !== registerFormData.value.comfirmPass
        ) {
          callback(new Error('两次输入的密码不匹配'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  isAgree: [
    {
      validator: (rule, value, callback) => {
        if (!value) {
          callback(new Error('请勾选同意隐私条款和服务条款'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
})

// 登录
const loginForm = ref(null)
const login = () => {
  loginForm.value.validate(async (isOk) => {
    if (isOk) {
      await userStore.login({
        account: loginFormData.value.account,
        password: loginFormData.value.password
      })
      // 跳转到首页或重定向页
      const redirect = route.query.redirect
      if (redirect) {
        router.replace(redirect)
      } else {
        router.replace('/')
      }
      // eslint-disable-next-line no-undef
      ElMessage.success('登录成功')
    }
  })
}

// 注册
const registerForm = ref(null)
const register = () => {
  registerForm.value.validate(async (isOk) => {
    if (isOk) {
      // 发送注册请求
      await registerApi({
        account: registerFormData.value.account,
        password: registerFormData.value.password
      })
      // eslint-disable-next-line no-undef
      ElMessage.success('注册成功')
      isRegister.value = false
      loginFormData.value.account = registerFormData.value.account
      loginFormData.value.password = registerFormData.value.password
    }
  })
}
</script>

<template>
  <div>
    <header class="login-header">
      <div class="container m-top-20">
        <h1 class="logo">
          <RouterLink to="/">小兔鲜</RouterLink>
        </h1>
        <RouterLink class="entry" to="/">
          进入网站首页
          <i class="iconfont icon-angle-right"></i>
          <i class="iconfont icon-angle-right"></i>
        </RouterLink>
      </div>
    </header>
    <section class="login-section">
      <div class="wrapper">
        <nav>
          <a v-if="!isRegister" href="javascript:;">账户登录</a>
          <a v-else href="javascript:;">账户注册</a>
        </nav>
        <div class="account-box">
          <div class="form">
            <el-form
              v-if="!isRegister"
              ref="loginForm"
              :model="loginFormData"
              :rules="loginRules"
              label-position="right"
              label-width="60px"
              status-icon
            >
              <el-form-item label="账户" prop="account">
                <el-input v-model="loginFormData.account" />
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="loginFormData.password" />
              </el-form-item>
              <el-form-item label-width="22px" prop="isAgree">
                <el-checkbox v-model="loginFormData.isAgree" size="large">
                  我已同意隐私条款和服务条款
                </el-checkbox>
              </el-form-item>
              <el-button size="large" class="subBtn" @click="login"
                >点击登录</el-button
              >
              <p style="margin-top: 5px">
                <el-link type="primary" @click="isRegister = true"
                  >没有账户？点击注册</el-link
                >
              </p>
            </el-form>
            <el-form
              v-else
              ref="registerForm"
              :model="registerFormData"
              :rules="registerRules"
              label-position="right"
              label-width="80px"
              status-icon
            >
              <el-form-item label="登录账户" prop="account">
                <el-input v-model="registerFormData.account" />
              </el-form-item>
              <el-form-item label="登录密码" prop="password">
                <el-input type="password" v-model="registerFormData.password" />
              </el-form-item>
              <el-form-item label="确认密码" prop="comfirmPass">
                <el-input
                  type="password"
                  v-model="registerFormData.comfirmPass"
                />
              </el-form-item>
              <el-form-item label-width="55px" prop="isAgree">
                <el-checkbox v-model="registerFormData.isAgree" size="large">
                  我已同意隐私条款和服务条款
                </el-checkbox>
              </el-form-item>
              <el-button size="large" class="subBtn" @click="register"
                >注册</el-button
              >
              <p style="margin-top: 5px">
                <el-link type="primary" @click="isRegister = false"
                  >切换登录</el-link
                >
              </p>
            </el-form>
          </div>
        </div>
      </div>
    </section>

    <footer class="login-footer">
      <div class="container">
        <p>
          <a href="javascript:;">关于我们</a>
          <a href="javascript:;">帮助中心</a>
          <a href="javascript:;">售后服务</a>
          <a href="javascript:;">配送与验收</a>
          <a href="javascript:;">商务合作</a>
          <a href="javascript:;">搜索推荐</a>
          <a href="javascript:;">友情链接</a>
        </p>
      </div>
    </footer>
  </div>
</template>

<style scoped lang="scss">
.login-header {
  background: #fff;
  border-bottom: 1px solid #e4e4e4;

  .container {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }

  .logo {
    width: 350px;

    a {
      display: block;
      height: 122px;
      width: auto;
      text-indent: -9999px;
      background: url('@/assets/images/logo.png') no-repeat center center / 90%;
    }
  }

  .sub {
    flex: 1;
    font-size: 24px;
    font-weight: normal;
    margin-bottom: 38px;
    margin-left: 20px;
    color: #666;
  }

  .entry {
    width: 120px;
    margin-bottom: 38px;
    font-size: 16px;

    i {
      font-size: 14px;
      color: $xtxColor;
      letter-spacing: -5px;
    }
  }
}

.login-section {
  background-image: linear-gradient(
    181.2deg,
    rgb(181, 239, 249) 10.5%,
    rgb(254, 254, 254) 86.8%
  );

  height: 538px;
  position: relative;
  // top: -5px;

  .wrapper {
    width: 380px;
    // background: rgb(90, 194, 253);
    position: absolute;
    left: 55%;
    top: 120px;
    transform: translate3d(100px, 0, 0);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);
    border-radius: 3px;
    opacity: 0.9;

    nav {
      font-size: 14px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
      text-align: right;
      align-items: center;

      a {
        flex: 1;
        line-height: 1;
        display: inline-block;
        font-size: 18px;
        position: relative;
        text-align: center;
      }
    }
  }
}

.login-footer {
  padding: 10px 0 30px;
  background: #fff;

  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      display: inline-block;

      ~ a {
        border-left: 1px solid #ccc;
      }
    }
  }
}

.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;

    a {
      color: $xtxColor;

      i {
        font-size: 14px;
      }
    }
  }

  .form {
    padding: 0 20px 20px 20px;

    &-item {
      margin-bottom: 28px;

      .input {
        position: relative;
        height: 36px;

        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }

        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;

          &.error {
            border-color: $priceColor;
          }

          &.active,
          &:focus {
            border-color: $xtxColor;
          }
        }

        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }

      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: $priceColor;

        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }

    .agree {
      a {
        color: #069;
      }
    }

    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: $xtxColor;

      &.disabled {
        background: #cfcdcd;
      }
    }
  }

  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}

.subBtn {
  background: $xtxColor;
  width: 100%;
  color: #fff;
}
</style>
